/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Facade;

import Entity.Company;
import Entity.User;
import Facade.exceptions.NonexistentEntityException;
import Service.CompanyService;
import VO.CompanyVO;
import VO.UserVO;
import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.EntityNotFoundException;
import javax.persistence.Persistence;
import javax.persistence.Query;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Root;

import service.UserService;
import java.util.ArrayList;
import javax.persistence.*;


/**
 *
 * @author Edwin
 */
public class StockFacade {
    
    private UserService serviceUser;
    private UserVO userVO;
    private User userEntity;
   // private StockService serviceStock; 
    

    public StockFacade() {
        serviceUser = new UserService();
        userVO = new UserVO();
        userEntity = new User();
        //serviceStock = new StockService();
    }       

    public StockFacade(EntityManagerFactory emf) {
        this.emf = emf;
    }
    
    private EntityManagerFactory emf = null;

    public EntityManager getEntityManager() {
        emf = Persistence.createEntityManagerFactory("ProyectoFinalPU2");
        return emf.createEntityManager();
    }

    public boolean create(CompanyVO comp) {
        EntityManager em = emf.createEntityManager();
        em.getTransaction().begin();
        boolean ret = false;
        try {
            //em.persist(object);
            //serviceStock.persistCompany(comp, em);
            em.getTransaction().commit();
            ret = true;
        } catch (Exception e) {
            e.printStackTrace();
            em.getTransaction().rollback();
            em.close();
        }
        return ret;
    }   
    /*
    public boolean destroy(StockVO comp) {
        EntityManager em = emf.createEntityManager();
        em.getTransaction().begin();
        boolean ret = false;
        try {
            serviceStock.removeStock(comp,em);
            //em.remove(comp);
            em.getTransaction().commit();
            ret = true;
        } catch (Exception e) {
            e.printStackTrace();
            em.getTransaction().rollback();
        } finally {
            em.close();
            return ret;
        }
    }*/

     public boolean eliminar(int referencia) {
        EntityManager em = emf.createEntityManager();
        em.getTransaction().begin();
        boolean ret = false;
        try {
            //.eliminarCompany(int referencia);
            
            Query q=em.createQuery("DELETE FROM Company WHERE id= :idCompany")
                    .setParameter("idCompany",referencia);
            q.executeUpdate();
            em.getTransaction().commit();
            ret = true;
        } catch (Exception e) {
            e.printStackTrace();
            em.getTransaction().rollback();
        } finally {
            em.close();
            return ret;
        }
    }

     /*
  public Stock read(int referencia) {
        EntityManager em = emf.createEntityManager();
        Stock company = null;
        Query q = em.createQuery("SELECT u FROM Company u " +
                    "WHERE u.id = :idCompany")
                    .setParameter("idCompany", referencia);
        try {
            stock = (Stock) q.getSingleResult();
        } catch (NonUniqueResultException e) {
            stock = (Stock) q.getResultList().get(0);
        } catch(NoResultException e){} 
        catch (Exception e){
            e.printStackTrace();
        }finally {
            em.close();
            return company;
        }
    }

  public Company leer(Stock par) {
        EntityManager em = emf.createEntityManager();
        Stock stock = null;
        Query q = em.createQuery("SELECT u FROM Company u " +
                    "WHERE u.id = :idCompany")
                    .setParameter("idCompany", par.getId());
        try {
            stock = (Company) q.getSingleResult();
        } catch (NonUniqueResultException e) {
            stock = (Company) q.getResultList().get(0);
        } catch (Exception e){
            e.printStackTrace();
        }finally {
            em.close();
            return stock;
        }
    }

   public ArrayList<Company> leer() {
        ArrayList<Company>company=new ArrayList();
        EntityManager em = emf.createEntityManager();
        Query q = em.createQuery("SELECT u FROM Docente u ");
        try {
            stock = (ArrayList<Stock>) q.getResultList();
        } catch (NoResultException e) {
        } catch (Exception e){
            e.printStackTrace();
        }finally {
            em.close();
            return stock;
        }
    }

    public boolean actualizar(Stock object, Stock nuevoObjeto) {
        Stock objeto = object;
        EntityManager em = emf.createEntityManager();
        em.getTransaction().begin();
        String name = nuevoObjeto.getNameCompany();
        String symbol = nuevoObjeto.getSymbolCompany();
        String description = nuevoObjeto.getDescriptionCompany();
        boolean ret = false;
        try {
            if(name!=null)
                objeto.setNameCompany(name);
            if(symbol!=null)
                objeto.setSymbolCompany(symbol);
            if(description!=null)
                objeto.setDescriptionCompany(description);
            em.merge(object);
            em.getTransaction().commit();
            ret = true;
        } catch (Exception e) {
            e.printStackTrace();
            em.getTransaction().rollback();
        } finally {
            em.close();
            return ret;
        }
    }
    */
}
